﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestEntity1
{
    public partial class EditCompany : Form
    {
        int? _ID;
        List<Company> _list;
        public EditCompany(int? ID, List<Company> list)
        {
            InitializeComponent();
            _ID = ID;
            _list = list;
        }

        private void EditCompany_Load(object sender, EventArgs e)
        {
            if (_ID != null)
            {

                var h = (from i in _list where i.ID ==(int)_ID select i).FirstOrDefault();
                textBox1.Text = h.Name;
                textBox2.Text = h.Director;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_ID != null)
            {
                var h = (from i in _list where i.ID == (int)_ID select i).FirstOrDefault();
                h.Name = textBox1.Text;
                h.Director = textBox2.Text;
            }
            else
            {
                var h = _list.Count > 0 ? _list.Max(i => i.ID) + 1 : 0;
                _list.Add(new Company() {Director = textBox2.Text, Name=textBox1.Text, ID=h });
            }
            EntityLayer.XmlHelper<Company> help = new EntityLayer.XmlHelper<Company>(CompanyEntity.ObjectXmlFile);
            help.Serialise(_list);
            this.Close();
        }
    }
}
